using System;
using bitzhuwei.Compiler;

namespace bitzhuwei.GLSLFormat {
    /// <summary>
    /// Correspond to the Vn node unary_expression in the grammar(GLSL).
    /// </summary>
    internal partial class Vnunary_expression : IFullFormat {
        // [032] unary_expression = postfix_expression ;
        // [033] unary_expression = '++' unary_expression ;
        // [034] unary_expression = '--' unary_expression ;
        // [035] unary_expression = unary_operator unary_expression ;


        private readonly List<IFullFormat> prefix = new();

        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;
        public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
            var config = new BlankConfig();
            var prefixCount = this.prefix.Count;
            for (var i = 0; i < prefixCount; i++) {
                context.PrintBlanksAnd(this.prefix[i], i == 0 ? preConfig : config, writer);
                if (i + 1 < prefixCount) {
                    context.PrintCommentsBetween(this.prefix[i], this.prefix[i + 1], config, writer);
                }
                else {
                    context.PrintCommentsBetween(this.prefix[i], this.r0, config, writer);
                }
            }
            context.PrintBlanksAnd(this.r0, prefixCount == 0 ? preConfig : config, writer);
        }

        // [033] unary_expression = '++' unary_expression ;
        public void Insert(Token r0) {
            this.prefix.Insert(0, new Prefix0(r0));
            this._scope.start = r0.index;
        }
        class Prefix0 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Token r0;
            public Prefix0(Token r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }

        // [034] unary_expression = '--' unary_expression ;
        // Pre/Postfix1 repeated with Pre/Postfix0

        // [035] unary_expression = unary_operator unary_expression ;
        public void Insert(Vnunary_operator r0) {
            this.prefix.Insert(0, new Prefix2(r0));
            this._scope.start = r0.Scope.start;
        }
        class Prefix2 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Vnunary_operator r0;
            public Prefix2(Vnunary_operator r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }


        public readonly Vnpostfix_expression r0;
        public Vnunary_expression(Vnpostfix_expression r0) {
            this.r0 = r0;
            this._scope = new TokenRange(r0);
        }


    }
}
